package com.lamp.listadapter;

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lamp.JsonClass.BusinessLists;
import com.lamp.downloadimage.BitmapCache;
import com.lamp.downloadimage.ImageDownloader;
import com.lamp.main.R;

public class CheckInAdapter extends ArrayAdapter<BusinessLists> {
	private final List<BusinessLists> list;
	private final Activity context;
	private ImageDownloader imageDownloader;

	public CheckInAdapter(Activity context, List<BusinessLists> list,
			BitmapCache bitmapCache) {
		super(context, R.layout.checkin_item, list);
		this.list = list;
		this.context = context;
		this.imageDownloader = new ImageDownloader(bitmapCache);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		ViewHolder holder;
		if (rowView == null) {
			LayoutInflater inflate = context.getLayoutInflater();
			rowView = inflate.inflate(R.layout.checkin_item, null, true);
			holder = new ViewHolder();
			holder.picture = (ImageView) rowView.findViewById(R.id.chphoto);
			holder.name = (TextView) rowView.findViewById(R.id.chname);
			holder.note = (TextView) rowView.findViewById(R.id.chcate);
			holder.date = (TextView) rowView.findViewById(R.id.chdistance);
			holder.arrow = (ImageView) rowView.findViewById(R.id.arrow);
			rowView.setTag(holder);
		} else {
			holder = (ViewHolder) rowView.getTag();
		}
		holder.arrow.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

			}
		});

		holder.name.setText(list.get(position).getBusinessName());
		holder.note.setText(list.get(position).getCategoryName());
		double distance = getDistance(list.get(position).getLongitude(), list
				.get(position).getLatitude());
		String locD = String.valueOf((int) distance + " m");
		if (distance > 100) {
			distance /= 1000;
			locD = String.valueOf(distance);
			locD = locD.substring(0, 4) + " km";
		}
		holder.date.setText(locD);

		imageDownloader.download(list.get(position).getPhoto(),
				(ImageView) holder.picture);
		rowView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		return rowView;
	}

	private double getDistance(double lng, double lat) {
		double distance;

		Location locationA = new Location("point A");

		locationA.setLatitude(lat);
		locationA.setLongitude(lng);

		Location locationB = new Location("point B");

		locationB.setLatitude(47.919837);
		locationB.setLongitude(106.929909);

		distance = locationB.distanceTo(locationA);
		return Math.round(distance);
	}

	static class ViewHolder {
		protected ImageView picture;
		protected TextView name;
		protected TextView note;
		protected TextView date;
		protected ImageView arrow;
	}
}
